package com.only.common.doucment;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * @author only
 * @date 2023/11/1
 * @description 原 OperaterResult 中 data 字段为 Object 类型，无法生成文档，这里重新进行实现
 */
@Data
public class Result<T> {
    /**
     * 请求是否成功
     */
    private boolean success;

    /**
     * 业务编码
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String code;

    /**
     * 额外的信息
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;

    /**
     * 返回的实际数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public Result(boolean success, String code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Result<Boolean> success() {
        return new Result<Boolean>(true, "", "", true);
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(true, "", "", data);
    }

    public static <T> Result<T> success(String code, T data) {
        return new Result<T>(true, code, "", data);
    }

    public static <T> Result<T> success(String code, String message, T data) {
        return new Result<T>(true, code, message, data);
    }

    public static Result<Boolean> fail() {
        return new Result<Boolean>(true, "", "", false);
    }
}
